<?php
/**
 * 销售退货单
 * Created by PhpStorm.
 * User: echa
 * Date: 2018/3/23
 * Time: 18:44
 */
namespace App\Http\Controllers\WarehouseManage;
use App\Http\Controllers\ApiController;
use App\Services\Warehouse\SalesReturnsService;
use Illuminate\Http\Request;

class SalesReturnsController extends ApiController{

    protected $SalesReturnsService;
    public function __construct(SalesReturnsService $salesReturnsService)
    {
        $this->SalesReturnsService=$salesReturnsService;
    }


    /***
     * 出仓后，客服操作退款
     * 参数说明：refund_data={"refund_type":1,"k3_number":"CK001","goodslist":[{"goods_sn":"0080687504565","spec_unit":"\u76d2","goods_number":1},{"goods_sn":"019800036591","spec_unit":"\u74f6","goods_number":2}]}
     * @param Request $request
    @author echa
     * @return mixed
     */
    public function SalesRefunds(Request $request){
        $refund_data=$request->refund_data;
        if(empty($refund_data)){
            return $this->failed('非法请求');
        }
        $refund_data=json_decode($refund_data,true);
        if(empty($refund_data)){
            return $this->failed('参数错误');
        }
        $refund_type=$refund_data['refund_type'];
        if(empty($refund_data['order_sn'])){
            $refund_data['order_sn']='';
        }
        if(empty($refund_data['freight'])){
            $refund_data['freight']=0;
        }
        if(empty($refund_data['discountamount'])){
            $refund_data['discountamount']=0;
        }
        $data=[
            'order_sn'=>$refund_data['order_sn'],
            'k3_number'=>$refund_data['k3_number'],
            'freight'=>$refund_data['freight'],
            'discountamount'=>$refund_data['discountamount'],
            'goods'=>$refund_data['goodslist']
        ];
        try{
            if($refund_type==1){ // 退货退款
                $return=$this->SalesReturnsService->SalesReturnRefunds($data,1);
            }else if($refund_type==2){ //漏送退款
                $return=$this->SalesReturnsService->MissFeedRefunds($data);
            }elseif($refund_type==3){//门店收银退货退款
                $return=$this->SalesReturnsService->SalesReturnRefunds($data,2);
            }else{
                return $this->failed('操作类型不存在');
            }
            if(empty($return['Id'])){
                return $this->failed('操作失败');
            }else{
                return $this->success($return);
            }
        }catch (\Exception $exception){
            return $this->failed($exception->getMessage());
        }

    }
}
